package gold.digger;

import gold.utils.InputUtil;
import gold.utils.UF;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1042 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[] gardenNoAdj(int n, int[][] paths) {
            Map<Integer, List<Integer>> nextMap = new HashMap<>();
            for (int[] path : paths) {
                if (!nextMap.containsKey(path[0])) nextMap.put(path[0], new ArrayList<>());
                nextMap.get(path[0]).add(path[1]);

                if (!nextMap.containsKey(path[1])) nextMap.put(path[1], new ArrayList<>());
                nextMap.get(path[1]).add(path[0]);
            }

            int[] ans = new int[n];
            for (int i = 1; i <= n; i++) dfs(i, 0, nextMap, ans);
            System.out.println(Arrays.toString(ans));
            return ans;
        }

        public void dfs(int cur, int prevColor, Map<Integer, List<Integer>> nextMap, int[] ans) {
            if (ans[cur - 1] > 0) return;
            List<Integer> nextNodeList = nextMap.get(cur);
            int curColor = getCurColor(cur, prevColor, nextNodeList, ans);
            ans[cur - 1] = curColor;
            if (null != nextNodeList && nextNodeList.size() > 0) {
                for (Integer next : nextNodeList) {
                    dfs(next, curColor, nextMap, ans);
                }
            }
        }

        public int getCurColor(int cur, int prevColor, List<Integer> nextNodeList, int[] ans) {
            Set<Integer> candidateColorSet = new HashSet<>(Arrays.asList(1, 2, 3, 4));
            candidateColorSet.remove(prevColor);
            if (null != nextNodeList && nextNodeList.size() > 0) {
                for (Integer next : nextNodeList) {
                    if (ans[next - 1] > 0) candidateColorSet.remove(ans[next - 1]);
                }
            }
            return candidateColorSet.iterator().next();
        }


    }


    class Solution_Wrong_Thought {

        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }

            /* 返回图中联通集合 */
            public Map<Integer, List<Integer>> outputAggregateSet(int n) {
                Map<Integer, List<Integer>> aggregateMap = new HashMap<>();

                // 共有n个集合
                for (int i = 0; i < n; i++) {
                    int parentId = find(i);
                    if (!aggregateMap.containsKey(parentId)) aggregateMap.put(parentId, new ArrayList<>());
                    aggregateMap.get(parentId).add(i);
                }

                System.out.println(aggregateMap.toString());
                return aggregateMap;
            }
        }

        public int[] gardenNoAdj(int n, int[][] paths) {
            UF uf = new UF(n + 1);
            for (int[] path : paths) {
                uf.union(path[0], path[1]);
            }

            Map<Integer, List<Integer>> sameGroup = new HashMap<>();
            for (int i = 1; i <= n; i++) {
                if (i == uf.parent[i]) continue;
                int parent = uf.parent[i];
                if (!sameGroup.containsKey(parent)) sameGroup.put(parent, new ArrayList<>());
                sameGroup.get(parent).add(i);
            }

            int[] ans = new int[n];
            for (Integer parent : sameGroup.keySet()) {
                int color = 1;
                ans[parent - 1] = color++;
                for (Integer child : sameGroup.get(parent)) {
                    ans[child - 1] = color++;
                }
            }

            System.out.println(Arrays.toString(ans));
            return ans;
        }
    }

    public void run() {
        System.out.println(new Solution().gardenNoAdj(3, InputUtil.toDoubleIntegerArray("[[1,2],[2,3],[3,1]]")));
        System.out.println(new Solution().gardenNoAdj(4, InputUtil.toDoubleIntegerArray("[[1,2],[3,4]]")));
        System.out.println(new Solution().gardenNoAdj(4, InputUtil.toDoubleIntegerArray("[[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]")));
        System.out.println(new Solution().gardenNoAdj(5, InputUtil.toDoubleIntegerArray("[[4,1],[4,2],[4,3],[2,5],[1,2],[1,5]]")));
    }

    public static void main(String[] args) throws Exception {
        LC1042 an = new LC1042();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
